<template>
  <!-- v-if="goodsDetail.goods_name" 来解决初始时， goodsDetail是空对象造成的undefined问题 -->
  <view v-if="goodsDetail.goods_name" class="good-details-container">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item, i) in goodsDetail.pics" :key="i">
        <image :src="item.pics_mid_url" @click="previewPic(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 商品名称 -->
    <view class="goods-content">
      <view class="goods-price">¥{{goodsDetail.goods_price}}</view>
      <view class="goods-box">
        <view class="goods-name">{{goodsDetail.goods_name}}</view>
        <view class="good-fav">
          <uni-icons type="star" size="17" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="goods-post">快递：免运费</view>
    </view>
    <!-- 图文详情 -->
    <!-- 服务器返回的是一串html文本， 所以需要通过富文本标签来解析 -->
    <rich-text :nodes="goodsDetail.goods_introduce" class="detail-pic"></rich-text>
    <!-- 商品导航 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('cart', ['carts']),
      ...mapGetters('cart', ['total'])
    },
    data() {
      return {
        goodsDetail: {},
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    watch: {
      total: {
        handler(newVal) {
          // 找到购物车的info 修改它的值
          const barge = this.options.find(x => x.text === '购物车')
          barge.info = newVal
        },
        immediate: true
      }
    },
    async onLoad(option) {
      console.log(option);
      const {
        data
      } = await uni.$http.get('/api/public/v1/goods/detail', {
        'goods_id': option.goods_id
      })
      if (data.meta && data.meta.status !== 200) return uni.$showMessage()
      // 由于返回的图片是webp的话， ios兼容有问题，所以我们需要修改所有的图片的后缀问jpg， 服务器根据我们的后缀给我们对应的图片
      data.message.goods_introduce = data.message.goods_introduce.replace(/<img/g, '<img style="display: block"')
        .replace(/webp/g, 'jpg')
      this.goodsDetail = data.message
    },
    methods: {
      ...mapMutations('cart', ['addToCart']),

      previewPic(i) {
        uni.previewImage({
          current: i,
          urls: this.goodsDetail.pics.map(x => x.pics_big)
        })
      },
      // 左侧点击事件
      onClick(e) {
        // 判断点击的是那个按钮，就跳转到那个页面
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        // 判断点击的是加入购物车
        if (e.content.text === '加入购物车') {
          // 整理这条商品的信息
          const goodInfo = {
            goods_id: this.goodsDetail.goods_id, // 商品的Id
            goods_name: this.goodsDetail.goods_name, // 商品的名称
            goods_price: this.goodsDetail.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goodsDetail.goods_small_logo, // 商品的图
            goods_state: true // 商品的勾选状态
          }
          // 调用vuex的方法，来修改cart数据
          this.addToCart(goodInfo)
        }
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-content {
    padding: 5px 10px;

    .goods-price {
      color: #C00000;
      font-size: 18px;
    }

    .goods-box {
      display: flex;
      justify-content: space-between;
      padding: 5px 0;

      .goods-name {
        margin-right: 5px;
        font-size: 13px;
      }

      .good-fav {
        width: 100px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        font-size: 13px;
        color: gray;
      }
    }

    .goods-post {
      font-size: 12px;
      color: gray;
    }
  }

  .good-details-container {
    padding-bottom: 50px;
  }

  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    // width: 100%;
  }
</style>
